package com.neusoft.neuiotms.dcam.common.converter;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcam.common.dto.DcamInstitutionsDTO;
import com.neusoft.neuiotms.dcam.common.model.DcamInstitutions;
import com.neusoft.neuiotms.dcam.common.model.DcamInstitutionsUser;
import com.neusoft.neuiotms.dcam.common.model.DcamResidentUser;
import com.neusoft.neuiotms.dcam.common.repository.DcamInstitutionsRepository;
import com.neusoft.neuiotms.dcam.common.repository.DcamInstitutionsUserRepository;
import com.neusoft.neuiotms.dcam.common.repository.DcamResidentUserRepository;

@Component
public class DcamInstitutionsConverter extends AbstractConverter<DcamInstitutions, DcamInstitutionsDTO> {
    @Autowired
    private DcamInstitutionsRepository dcamInstitutionsRepository;
    @Autowired
    private BaseIdGenerator institutionsCodeGenerator;
    @Autowired
    private DcamResidentUserRepository dcamResidentUserRepository;
    @Autowired
    private DcamInstitutionsUserRepository dcamInstitutionsUserRepository;
    @Autowired
    private DcamResidentUserConverter dcamResidentUserConverter;
    @Autowired
    private DcamInstitutionsUserConverter dcamInstitutionsUserConverter;

    @Override
    public DcamInstitutions toModel(final DcamInstitutionsDTO dto) {
        DcamInstitutions model;
        if (dto.isNew()) {
            model = new DcamInstitutions();
            model.setCode(this.institutionsCodeGenerator.generateCode());
        } else {
            model = this.dcamInstitutionsRepository.getOne(dto.getId());
            model.setCode(dto.getCode());
        }

        model.setName(dto.getName());
        //        model.setResidentUserCode(dto.getResidentUserCode());
        //        model.setResidentUserName(dto.getResidentUserName());

        final List<DcamResidentUser> residents = new ArrayList<>(model.getResidents());

        residents.forEach(k -> {
            model.getResidents().remove(k);
            this.dcamResidentUserRepository.delete(k);
        });

        dto.getResidents().stream().map(s -> {
            final DcamResidentUser user = this.dcamResidentUserConverter.toModel(s);
            user.setInstitutions(model);
            return user;
        }).forEach(model.getResidents()::add);

        final List<DcamInstitutionsUser> users = new ArrayList<>(model.getUsers());

        users.forEach(k -> {
            model.getUsers().remove(k);
            this.dcamInstitutionsUserRepository.delete(k);
        });

        dto.getUsers().stream().map(s -> {
            final DcamInstitutionsUser user = this.dcamInstitutionsUserConverter.toModel(s);
            user.setInstitutions(model);
            return user;
        }).forEach(model.getUsers()::add);

        return model;
    }

    @Override
    public DcamInstitutionsDTO toDTO(final DcamInstitutions model, final boolean forListView) {
        final DcamInstitutionsDTO dto = new DcamInstitutionsDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        //        dto.setResidentUserCode(model.getResidentUserCode());InstitutionsUserConverter
        //        dto.setResidentUserName(model.getResidentUserName());

        if (null != model.getResidents()) {
            model.getResidents().stream().map(l -> this.dcamResidentUserConverter.toDTO(l))
                    .forEach(dto.getResidents()::add);
        }
        if (null != model.getUsers()) {
            model.getUsers().stream().map(l -> this.dcamInstitutionsUserConverter.toDTO(l))
                    .forEach(dto.getUsers()::add);
        }

        return dto;
    }
}
